package only.my.railgunys.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import only.my.railgunys.artifact.*;
import only.my.railgunys.artifact.calc.ArtifactCalc;
import only.my.railgunys.bean.db.Artifact;
import only.my.railgunys.bean.db.PossibleFinalForm;
import only.my.railgunys.repository.ArtifactRepository;
import only.my.railgunys.repository.PossibleFinalFormRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class DataImporter {
    @Autowired
    ArtifactRepository artifactRepository;
    @Autowired
    PossibleFinalFormRepository possibleFinalFormRepository;

//    @PostConstruct
    public void importArtifactFromFile() throws IOException {
        String outFileName = "D:\\data\\artifacts.json";
        File file = new File(outFileName);
        if(file.exists()){
            ObjectMapper objectMapper = new ObjectMapper();
            ArtifactDto[] dtos = objectMapper.readValue(file, ArtifactDto[].class);
            List<ArtifactDto> artifactDtoList  = Arrays.asList(dtos);
            List<Artifact> collect = artifactDtoList.stream().map(DataImporter::transferDtoToBean).collect(Collectors.toList());
            artifactRepository.saveAll(collect);
        }
        exhaustivePossibilities();
    }
    public void exhaustivePossibilities(){
        List<Artifact> all = artifactRepository.findAll();
        List<Artifact> artifactList = all.stream().filter(artifact -> artifact.getLevel() <= 16).filter(artifact -> artifact.getSubStat4Type() > 0).collect(Collectors.toList());
        artifactList.forEach(artifact -> possibleFinalFormRepository.saveAll(ArtifactCalc.upgradeToFinal(artifact)));
    }


    public static Artifact transferDtoToBean(ArtifactDto dto){
        Artifact artifact = new Artifact();

        artifact.setLevel(dto.getLevel());
        artifact.setAsKey(ArtifactSuit.valueOf(dto.getAsKey()).getCode());

        String mainType = dto.getMainStat();
        artifact.setMainStat(mainType);
        artifact.setMainStatCode(ArtifactMain.valueOf(mainType).getCode());

        String slot = dto.getSlot();
        artifact.setSlot(ArtifactSlot.valueOf(slot).getCode());

        String subStat1Type = dto.getSubStat1Type();
        artifact.setSubStat1Type(ArtifactSub.valueOf(subStat1Type).getCode());
        artifact.setSubStat1Value(dto.getSubStat1Value());

        String subStat2Type = dto.getSubStat2Type();
        artifact.setSubStat2Type(ArtifactSub.valueOf(subStat2Type).getCode());
        artifact.setSubStat2Value(dto.getSubStat2Value());

        String subStat3Type = dto.getSubStat3Type();
        artifact.setSubStat3Type(ArtifactSub.valueOf(subStat3Type).getCode());
        artifact.setSubStat3Value(dto.getSubStat3Value());

        String subStat4Type = dto.getSubStat4Type();

        if (subStat4Type != null) {
            artifact.setSubStat4Type(ArtifactSub.valueOf(subStat4Type).getCode());
            artifact.setSubStat4Value(dto.getSubStat4Value());
        }

        return  artifact;
    }
}
